package com.fr.report.cellElement.core;

import com.fr.base.FRContext;
import com.fr.base.core.list.IntList;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.script.Calculator;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MonoGrouper extends RecordGrouper
{
  public Group[] group(TableData paramTableData, int paramInt, int[] paramArrayOfInt, Calculator paramCalculator)
  {
    if (paramInt == -1)
      return super.group(paramTableData, paramInt, paramArrayOfInt, paramCalculator);
    if (paramArrayOfInt == null)
      try
      {
        paramArrayOfInt = IntList.range(paramTableData.getRowCount());
      }
      catch (TableDataException localTableDataException1)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
      }
    Group[] arrayOfGroup = new Group[paramArrayOfInt.length];
    for (int i = 0; i < paramArrayOfInt.length; ++i)
    {
      int j = paramArrayOfInt[i];
      try
      {
        arrayOfGroup[i] = new Group(paramTableData.getValueAt(j, paramInt), new int[] { j });
      }
      catch (TableDataException localTableDataException2)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
      }
    }
    return arrayOfGroup;
  }

  public Iterator groupIterator(TableData paramTableData, int paramInt, int[] paramArrayOfInt, Calculator paramCalculator)
  {
    return new MonoIterator(paramTableData, paramInt, paramArrayOfInt, paramCalculator);
  }

  private static class MonoIterator
    implements Iterator
  {
    private TableData data;
    private int columnIndex;
    private int[] rows;
    private Calculator calculator;
    private int nextArrayIndex = -1;
    private int nextTableDataIndex = -1;
    private Group nextGroup;

    MonoIterator(TableData paramTableData, int paramInt, int[] paramArrayOfInt, Calculator paramCalculator)
    {
      this.data = paramTableData;
      this.columnIndex = paramInt;
      this.rows = paramArrayOfInt;
      this.calculator = paramCalculator;
      findNext();
    }

    private void findNext()
    {
      if (this.rows == null)
      {
        this.nextTableDataIndex += 1;
      }
      else
      {
        this.nextArrayIndex += 1;
        if (this.nextArrayIndex >= this.rows.length)
          this.nextTableDataIndex = -1;
        else
          this.nextTableDataIndex = this.rows[this.nextArrayIndex];
      }
      try
      {
        if ((this.nextTableDataIndex < 0) || (!(this.data.hasRow(this.nextTableDataIndex))))
          this.nextGroup = null;
        else if (this.columnIndex == -1)
          this.nextGroup = new Group(new Integer(this.nextTableDataIndex + 1), new int[] { this.nextTableDataIndex });
        else
          this.nextGroup = new Group(this.data.getValueAt(this.nextTableDataIndex, this.columnIndex), new int[] { this.nextTableDataIndex });
      }
      catch (TableDataException localTableDataException)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
        this.nextGroup = null;
      }
    }

    public boolean hasNext()
    {
      return (this.nextGroup != null);
    }

    public Object next()
    {
      Group localGroup = this.nextGroup;
      findNext();
      return localGroup;
    }

    public void remove()
    {
      throw new UnsupportedOperationException();
    }
  }
}